/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Channel;
import com.cmcc.zysoft.infocollection.model.Industry;
import com.cmcc.zysoft.infocollection.model.ResourceProperty;
import com.cmcc.zysoft.infocollection.service.ChannelPcService;
import com.cmcc.zysoft.infocollection.service.IndustryPcService;
import com.cmcc.zysoft.infocollection.service.ResourcePropertyPcService;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: ResourcePropertyController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-14 下午4:31:45
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */


@Controller
@RequestMapping("/pc/property")
public class ResourcePropertyController extends BaseController {
	
	/**
	 * 属性名称: channelPcService.
	 * 类       型: ChannelPcService.
	 */
	@Resource
	private ChannelPcService channelPcService;

	/**
	 * 属性名称: industryPcService.
	 * 类       型: IndustryPcService.
	 */
	@Resource
	private IndustryPcService industryPcService;
	
	/**
	 * 属性名称: resourcePropertyPcService.
	 * 类       型: ResourcePropertyPcService.
	 */
	@Resource
	private ResourcePropertyPcService resourcePropertyPcService;
	
	/**
	 * 模板管理页面.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/property/propertyConfig";
	}
	
	/**
	 * 根据行业ID获取资源属性分页列表信息.
	 * @param industryId 行业ID
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return Map<String, Object>
	 */
	@RequestMapping("/list.htm")
	@ResponseBody
	public Map<String, Object> getPropertyList(String industryId, int page, int rows) {
		return this.resourcePropertyPcService.getPropertyList(industryId, page, rows);
	}
	
	/**
	 * 添加资源属性页面.
	 * @param modelMap modelMap
	 * @param industryId 行业ID
	 * @return String.
	 */
	@RequestMapping("/add.htm")
	public String add(ModelMap modelMap, String industryId) {
		Industry industry = this.industryPcService.getEntity(industryId);
		modelMap.addAttribute("industryId", industryId);
		modelMap.addAttribute("industryName", industry.getIndustryName());
		return "/pc/property/add";
	}
	
	/**
	 * 修改资源属性信息页面.
	 * @param modelMap modelMap
	 * @param industryId 行业ID
	 * @param propertyId 资源属性ID
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String edit(ModelMap modelMap, String industryId, String propertyId) {
		Industry industry = this.industryPcService.getEntity(industryId);
		ResourceProperty property = this.resourcePropertyPcService.getEntity(propertyId);
		modelMap.addAttribute("property", property);
		modelMap.addAttribute("industryId", industryId);
		modelMap.addAttribute("industryName", industry.getIndustryName());
		return "/pc/property/edit";
	}
	
	/**
	 * 保存资源属性信息.
	 * @param property 资源属性信息
	 * @param industryId 行业ID
	 * @param addAllFlag 是否添加全部标识(0-是-为该行业所属频道下的所有行业添加此属性,1-否-为指定行业添加资源属性信息)
	 * @return String
	 */
	@RequestMapping("/saveProperty.htm")
	@ResponseBody
	@Transactional
	public String addProperty(ResourceProperty property, String industryId, String addAllFlag) {
		Industry industry = this.industryPcService.getEntity(industryId);
		if("0".equals(addAllFlag)) { //添加全部-为该行业所属频道下的所有行业添加此属性
			//频道ID
			String channelId = industry.getChannel().getChannelId();
			Channel channel = this.channelPcService.getEntity(channelId);
			//获取该行业所属频道下的所有行业
			List<Industry> list = this.industryPcService.findByNamedParam("channel", channel);
			if(null != list && !list.isEmpty()) {
				for(int i = 0; i < list.size(); i++) {
					ResourceProperty resourceProperty = new ResourceProperty();
					resourceProperty.setIndustry(list.get(i));
					resourceProperty.setResourcePropertyName(property.getResourcePropertyName());
					resourceProperty.setResourcePropertyEnname(property.getResourcePropertyEnname());
					resourceProperty.setResourcePropertyFlag(property.getResourcePropertyFlag());
					this.resourcePropertyPcService.insertEntity(resourceProperty);
				}
			}
		} else { //为指定行业添加资源属性
			property.setIndustry(industry);
			this.resourcePropertyPcService.insertEntity(property);
		}
		return "SUCCESS";
	}
	
	/**
	 * 更新资源属性信息.
	 * @param property 资源属性信息
	 * @param industryId 行业ID
	 * @param updateAllFlag 是否修改全部标识(0-是-为该行业所属频道下的所有行业添加此属性,1-否-更新指定行业的资源属性信息)
	 * @return String
	 */
	@RequestMapping("/editProperty.htm")
	@ResponseBody
	@Transactional
	public String updateProperty(ResourceProperty property, String industryId, String updateAllFlag) {
		Industry industry = this.industryPcService.getEntity(industryId);
		if("0".equals(updateAllFlag)) { //修改全部-更新该行业所属频道下所有行业的此属性信息
			//频道ID
			String channelId = industry.getChannel().getChannelId();
			Channel channel = this.channelPcService.getEntity(channelId);
			//获取该行业所属频道下的所有行业
			List<Industry> list = this.industryPcService.findByNamedParam("channel", channel);
			if(null != list && !list.isEmpty()) {
				for(int i = 0; i < list.size(); i++) {
					//根据行业信息和资源属性代码查找资源属性信息
					ResourceProperty resourceProperty = this.resourcePropertyPcService.getResourceProperty(list.get(i), property.getResourcePropertyEnname());
					resourceProperty.setIndustry(list.get(i));
					resourceProperty.setResourcePropertyName(property.getResourcePropertyName());
					resourceProperty.setResourcePropertyEnname(property.getResourcePropertyEnname());
					resourceProperty.setResourcePropertyFlag(property.getResourcePropertyFlag());
					this.resourcePropertyPcService.updateEntity(resourceProperty);
				}
			}
		} else { //更新指定行业的资源属性信息
			property.setIndustry(industry);
			this.resourcePropertyPcService.updateEntity(property);
		}
		return "SUCCESS";
	}
	
	/**
	 * 删除资源属性.
	 * @param propertyIds 属性ID集合
	 * @return String
	 */
	@RequestMapping("/remove.htm")
	@ResponseBody
	@Transactional
	public String remove(String propertyIds) {
		String[] array = propertyIds.split(",");
		for(int i = 0; i < array.length; i++) {
			this.resourcePropertyPcService.deleteEntity(array[i]);
		}
		return "SUCCESS";
	}
	
	/**
	 * 检查同一行业下资源属性名称是否已存在.
	 * @param industryId 行业ID
	 * @param propertyName 资源属性名称
	 * @param propertyId 资源属性ID
	 * @return map
	 */
	@RequestMapping("/checkName.htm")
	@ResponseBody
	public Map<String, Object> checkPropertyName(String industryId, String propertyName, String propertyId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isExist = this.resourcePropertyPcService.checkPropertyName(industryId, propertyName, propertyId);
		if(isExist) {
			map.put("code", "0");
		} else {
			map.put("code", "1");
		}
		return map;
	}
	
	/**
	 * 检查同一行业下资源属性代码是否已存在.
	 * @param industryId 行业ID
	 * @param propertyCode 资源属性代码
	 * @param propertyId 资源属性ID
	 * @return map
	 */
	@RequestMapping("/checkCode.htm")
	@ResponseBody
	public Map<String, Object> checkPropertyCode(String industryId, String propertyCode, String propertyId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isExist = this.resourcePropertyPcService.checkPropertyCode(industryId, propertyCode, propertyId);
		if(isExist) {
			map.put("code", "0");
		} else {
			map.put("code", "1");
		}
		return map;
	}
}